package com.tanhua.server.controller;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.db.UserApi;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

/*    1、请求方式  POST
    2、请求地址  /user/loginReginfo/head
    3、请求参数
       请求头 Authorization里面放的是token值
       请求体文件类型的参数 headPhoto   用MultipartFile接收
    4、返回结果 无
    */
    @PostMapping(value = "/loginReginfo/head",name = "上传头像")
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws Exception{
        return userService.loginReginfoHead(headPhoto);
    }



//    完善用户信息

    /*    1、请求方式  POST
        2、请求地址  /user/loginReginfo
        3、请求参数
           请求头 Authorization里面放的是token值
           请求体 使用UserInfo接收
        4、返回结果 无
        */
    @PostMapping(value = "/loginReginfo",name = "完善用户信息")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token) throws Exception{
        return userService.loginReginfo(userInfo,token);
    }

//    POST/user/login
//    Headers中定义了请求数据格式：JSON
//  请求的数据在body中：  {"phone":"1380001380"}
//  返回结果：无
    @PostMapping(value = "/login",name = "登录时发送验证码")
    public ResponseEntity sendCode(@RequestBody Map map) throws Exception{
       String phone = (String) map.get("phone");
       return userService.sendCode(phone);
    }
//    POST/user/loginVerification
//    请求的参数在body中：{"phone":"13800138000","verificationCode":"123456"}
//    返回的数据json：{"token":"qegshreerscbcc","isNew":true}
    @PostMapping(value = "/loginVerification",name = "验证码校验完成登录")
    public ResponseEntity loginVerification(@RequestBody Map map) throws Exception{
        String phone = (String) map.get("phone");
        String verificationCode = (String) map.get("verificationCode");
        return userService.loginVerification(phone,verificationCode);
    }

}
